﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ErrorDumper.Processing {
    public static class ExceptionProcessors {

        private static readonly object _syncRoot = new object();
        private static readonly List<IExceptionProcessor> _processors =
            new List<IExceptionProcessor>();

        public static bool RegisterProcessor<TProcessor>()
            where TProcessor : class, IExceptionProcessor, new() {
            lock (_syncRoot) {
                if (_processors.Any(i => i.GetType() == typeof(TProcessor)))
                    return false;
                _processors.Add(new TProcessor());
                return true;
            }
        }

        public static ICollection<IExceptionProcessor> GetProcessors() {
            lock (_syncRoot)
                return _processors.ToArray();
        }

        public static void ProcessException(ExceptionProcessorContext context) {
            if (context == null)
                throw new ArgumentNullException("context");
            foreach (var processor in GetProcessors())
                processor.ProcessException(context);
        }
    }
}